// pages/user/cooperation/index.js
import citys from '../../../resource/data/pc.js';
import {
  getAuthCode
} from '../../../api/user.js';
import {
  applyTrainingCooperation,
  getTrainingCooperation
} from '../../../api/other.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isApply: false,
    telephone: '',
    cityOptions: citys,
    isShowCitys: false,
    name: '',
    idCard: '',
    area: '',
    companyName: '',
    businessNum: '',
    mobile: '',
    authCode: '',
    authBtnText: '获取验证码',
    countdown: 60,
    isSend: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // wx.showModal({
    //   content:"成功",
    //   cancelColor: 'cancelColor',
    // })
    getTrainingCooperation(res => {
      this.setData({
        isApply: res.isApply,
        telephone: res.mobile,
      })
    })
  },

  /**
   * 提交申请
   */
  handleCommitApply() {
    if (this.data.name === '') {
      this.showToast('请输入姓名');
    } else if (!(/(^\d{15}$)|(^\d{17}([0-9]|X|x)$)/.test(this.data.idCard))) {
      this.showToast('请输入正确格式的身份证号码');
    } else if (this.data.area === '') {
      this.showToast('请选择所在地区');
    } else if (this.data.companyName === '') {
      this.showToast('请输入企业名称');
    } else if (this.data.businessNum === '') {
      this.showToast('请输入营业执照编号');
    } else if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(this.data.mobile)) {
      this.showToast('请输入正确的手机号');
    } else if (this.data.authCode === '') {
      this.showToast('请输入验证码');
    } else {
      applyTrainingCooperation({
        name: this.data.name,
        idCard: this.data.idCard,
        address: this.data.address,
        company: this.data.companyName,
        taxNum: this.data.taxNum,
        phone: this.data.mobile,
        code: this.data.authCode,
      }).then(() => {
        this.setData({
          isApply: true,
        })
      })
    }
  },
  onClosed1(){
    this.setData({
      isApply:false
    })
  },
  /**
   * 发送验证码
   */
  handleSendAuthCode() {
    if (!/^1[3|4|5|6|8|7|9][0-9]\d{8}$/.test(this.data.mobile)) {
      this.showToast('请输入正确的手机号');
    } else {
      getAuthCode(this.data.mobile);
      this.countDown();
    }
  },

  /**
   * 倒计时
   */
  countDown() {
    if (this.data.countdown === 0) {
      this.setData({
        isSend: false,
        authBtnText: '重新获取',
        countdown: 60,
      })
      return
    } else {
      this.setData({
        isSend: true,
        authBtnText: `${this.data.countdown--}s`
      })
    }
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.countDown()
    }, 1000)
  },

  /**
   * 选择地区
   * @param {object} e 
   */
  handleChangeCitys(e) {
    let label = e.detail.options.map((n) => n.label).join(' ');
    let value = e.detail.options.map((n) => n.value);
    if (e.detail.options.length === 2) {
      this.setData({
        area: label,
        isShowCitys: false
      })
    }
  },

  handleOpenCascasder() {
    this.setData({
      isShowCitys: true
    })
  },

  /**
   * 姓名
   * @param {object} e 
   */
  onInputName(e) {
    this.data.name = e.detail.value.trim();
  },

  /**
   * 身份证号
   * @param {object} e 
   */
  onInputIDCard(e) {
    this.data.idCard = e.detail.value.trim();
  },

  /**
   * 企业名称
   * @param {object} e 
   */
  onInputCompanyName(e) {
    this.data.companyName = e.detail.value.trim();
  },

  /**
   * 营业执照编号
   * @param {object} e 
   */
  onInputBusinessNum(e) {
    this.data.businessNum = e.detail.value.trim();
  },

  /**
   * 手机号
   * @param {object} e 
   */
  onInputMobile(e) {
    this.data.mobile = e.detail.value.trim();
  },

  /**
   * 验证码
   * @param {object} e 
   */
  onInputAuthCode(e) {
    this.data.authCode = e.detail.value.trim();
  },

  /**
   * 吐司
   * @param {string} msg 
   */
  showToast(msg) {
    wx.showToast({
      title: msg,
      icon: 'none'
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})